package com.am.mosl.cityandroid.widget;

import java.lang.reflect.Field;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ListView;
import android.widget.SectionIndexer;
import android.widget.TextView;

import com.am.mosl.cityandroid.citychange.CityChangeActivity;

/**
* Created by mosl on 14-7-11.
*/
public class SideBar extends View {

    private Context context;
    private char[] mChars;
    private ListView mList;
    private TextView mDialog;
    private int mItemHeight;
    private SectionIndexer mSectionIndexer = null;

    public SideBar(Context context) {
        super(context);
        this.context = context;
        init();
    }

    public SideBar(Context context, AttributeSet attrs) {

        super(context, attrs);
        init();
        Display display = ((CityChangeActivity) context).getWindowManager()
                .getDefaultDisplay();
        int height = display.getHeight();
        mItemHeight = (height - dipTopx(context, 45) - getStatusBarHeight(context))
                / mChars.length;
        Log.i("zcc", "每一个的高度" + mItemHeight);
    }

    // dp转px
    public int dipTopx(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    public SideBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public void setListView(ListView list) {
        this.mList = list;
        mSectionIndexer = (SectionIndexer) list.getAdapter();
    }

    public void setTextView(TextView mDialogText) {
        this.mDialog = mDialogText;
    }

    private void init() {
        mChars = new char[] { '#', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
                'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
                'V', 'W', 'X', 'Y', 'Z' };

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        int y = (int) event.getY();
        int idx = y / mItemHeight;
        if (idx >= mChars.length) {
            idx = mChars.length - 1;
        } else if (idx < 0) {
            idx = 0;
        }
        if (event.getAction() == MotionEvent.ACTION_DOWN
                || event.getAction() == MotionEvent.ACTION_MOVE) {
            mDialog.setVisibility(View.VISIBLE);
            mDialog.setText("" + mChars[idx]);
            if (mSectionIndexer == null) {
                mSectionIndexer = (SectionIndexer) mList.getAdapter();
            }
            int position = mSectionIndexer.getSectionForPosition(mChars[idx]);
            if (position == -1) {
                return true;
            }
            mList.setSelection(position);
        } else {
            mDialog.setVisibility(View.INVISIBLE);
        }
        return true;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint();
        paint.setColor(0xff595c61);
        paint.setTextSize(mItemHeight * 0.6f);
        paint.setTextAlign(Paint.Align.CENTER);
        float widthCenter = getMeasuredWidth() / 2;
        for (int i = 0; i < mChars.length; i++) {
            canvas.drawText(String.valueOf(mChars[i]), widthCenter, mItemHeight
                    + (i * mItemHeight), paint);
        }
    }

    // 通知栏高度
    public static int getStatusBarHeight(Context context) {

        Class<?> c = null;

        Object obj = null;

        Field field = null;

        int x = 0, statusBarHeight = 0;

        try {
            c = Class.forName("com.android.internal.R$dimen");
            obj = c.newInstance();
            field = c.getField("status_bar_height");
            x = Integer.parseInt(field.get(obj).toString());
            statusBarHeight = context.getResources().getDimensionPixelSize(x);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return statusBarHeight;

    }
}